package com.atguigu.gulimall.gulimallproduct.service.impl;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.gulimallproduct.service.CategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.gulimallproduct.dao.CategoryDao;
import com.atguigu.gulimall.gulimallproduct.entity.CategoryEntity;
import com.atguigu.gulimall.gulimallproduct.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    @Autowired
    CategoryDao categoryDao;
    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> categoryEntities = categoryDao.selectList(null);
        List<CategoryEntity> categoryTree = categoryEntities.stream()
                .filter(categoryEntity -> categoryEntity.getCatLevel() == 1)
                .peek(categoryEntity -> categoryEntity.setChildren(getChildren(categoryEntity, categoryEntities)))
                .sorted((o1, o2) -> {
                    int sort1 = Optional.ofNullable(o1.getSort()).orElse(0);
                    int sort2 = Optional.ofNullable(o2.getSort()).orElse(0);
                    return sort1 - sort2;
                })
                .collect(Collectors.toList());
        return categoryTree;
    }

    @Override
    public Long[] findCategoryPath(Long categoryId) {
        List<Long> categoryPath = new ArrayList<>();
        List<Long> parentPath = findById(categoryId, categoryPath);
        Collections.reverse(parentPath);
        return parentPath.toArray(new Long[0]);
    }

    @Transactional
    @Override
    public void updateDetail(CategoryEntity category) {
        this.updateById(category);

        if (!StringUtils.isEmpty(category.getName())) {
            categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
        }
    }

    private List<Long> findById(Long categoryId, List<Long> categoryPath) {
        categoryPath.add(categoryId);
        CategoryEntity categoryEntity = getById(categoryId);
        if (categoryEntity.getParentCid() != 0) {
            findById(categoryEntity.getParentCid(), categoryPath);
        }
        return categoryPath;
    }

    private List<CategoryEntity> getChildren(CategoryEntity root, List<CategoryEntity> categoryEntities) {
        List<CategoryEntity> children = categoryEntities.stream()
                .filter(categoryEntity -> Objects.equals(categoryEntity.getParentCid(), root.getCatId()))
                .peek(categoryEntity -> categoryEntity.setChildren(getChildren(categoryEntity, categoryEntities)))
                .sorted((o1, o2) -> {
                    int sort1 = Optional.ofNullable(o1.getSort()).orElse(0);
                    int sort2 = Optional.ofNullable(o2.getSort()).orElse(0);
                    return sort1 - sort2;
                })
                .collect(Collectors.toList());
        return children;
    }

}